home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / GAS211S2.ZIP / src / gas-211 / opcodes / sparc-di.c < prev    next >
C/C++ Source or Header  |  1993-05-30  |  13KB  |  517 lines

  1. /* Print SPARC instructions.
  2.    Copyright 1989, 1991, 1992, 1993 Free Software Foundation, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include "opcode/sparc.h"
  19. #include "dis-asm.h"
  20. #include <string.h>
  21.  
  22. static  char *reg_names[] =
  23.  { "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",    
  24.   "o0", "o1", "o2", "o3", "o4", "o5", "sp", "o7",    
  25.   "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",    
  26.   "i0", "i1", "i2", "i3", "i4", "i5", "fp", "i7",    
  27.   "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",    
  28.   "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",    
  29.   "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
  30.   "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
  31.   "y", "psr", "wim", "tbr", "pc", "npc", "fpsr", "cpsr" };
  32.  
  33. #define    freg_names    (®_names[4 * 8])
  34.  
  35. /* FIXME--need to deal with byte order (probably using masking and
  36.    shifting rather than bitfields is easiest).  */
  37.  
  38. union sparc_insn
  39.   {
  40.     unsigned long int code;
  41.     struct
  42.       {
  43.     unsigned int anop:2;
  44. #define    op    ldst.anop
  45.     unsigned int anrd:5;
  46. #define    rd    ldst.anrd
  47.     unsigned int op3:6;
  48.     unsigned int anrs1:5;
  49. #define    rs1    ldst.anrs1
  50.     unsigned int i:1;
  51.     unsigned int anasi:8;
  52. #define    asi    ldst.anasi
  53.     unsigned int anrs2:5;
  54. #define    rs2    ldst.anrs2
  55. #define    shcnt    rs2
  56.       } ldst;
  57.     struct
  58.       {
  59.     unsigned int anop:2, anrd:5, op3:6, anrs1:5, i:1;
  60.     unsigned int IMM13:13;
  61. #define    imm13    IMM13.IMM13
  62.       } IMM13;
  63.     struct
  64.       {
  65.     unsigned int anop:2;
  66.     unsigned int a:1;
  67.     unsigned int cond:4;
  68.     unsigned int op2:3;
  69.     unsigned int DISP22:22;
  70. #define    disp22    branch.DISP22
  71.       } branch;
  72.  
  73. #define    imm22    disp22
  74.     struct
  75.       {
  76.     unsigned int anop:2;
  77.     unsigned int adisp30:30;
  78. #define    disp30    call.adisp30
  79.       } call;
  80.   };
  81.  
  82. /* Nonzero if INSN is the opcode for a delayed branch.  */
  83. static int
  84. is_delayed_branch (insn)
  85.      union sparc_insn insn;
  86. {
  87.   unsigned int i;
  88.  
  89.   for (i = 0; i < NUMOPCODES; ++i)
  90.     {
  91.       CONST struct sparc_opcode *opcode = &sparc_opcodes[i];
  92.       if ((opcode->match & insn.code) == opcode->match
  93.       && (opcode->lose & insn.code) == 0)
  94.     return (opcode->flags & F_DELAYED);
  95.     }
  96.   return 0;
  97. }
  98.  
  99. static int opcodes_sorted = 0;
  100. extern void qsort ();
  101.  
  102. /* Print one instruction from MEMADDR on STREAM.
  103.  
  104.    We suffix the instruction with a comment that gives the absolute
  105.    address involved, as well as its symbolic form, if the instruction
  106.    is preceded by a findable `sethi' and it either adds an immediate
  107.    displacement to that register, or it is an `add' or `or' instruction
  108.    on that register.  */
  109. int
  110. print_insn_sparc (memaddr, info)
  111.      bfd_vma memaddr;
  112.      disassemble_info *info;
  113. {
  114.   FILE *stream = info->stream;
  115.   union sparc_insn insn;
  116.  
  117.   register unsigned int i;
  118.  
  119.   if (!opcodes_sorted)
  120.     {
  121.       static int compare_opcodes ();
  122.       qsort ((char *) sparc_opcodes, NUMOPCODES,
  123.          sizeof (sparc_opcodes[0]), compare_opcodes);
  124.       opcodes_sorted = 1;
  125.     }
  126.  
  127.   {
  128.     int status =
  129.       (*info->read_memory_func) (memaddr, (bfd_byte *) &insn, sizeof (insn), info);
  130.     if (status != 0)
  131.       {
  132.     (*info->memory_error_func) (status, memaddr, info);
  133.     return -1;
  134.       }
  135.   }
  136.  
  137.   for (i = 0; i < NUMOPCODES; ++i)
  138.     {
  139.       CONST struct sparc_opcode *opcode = &sparc_opcodes[i];
  140.       if ((opcode->match & insn.code) == opcode->match
  141.       && (opcode->lose & insn.code) == 0)
  142.     {
  143.       /* Nonzero means that we have found an instruction which has
  144.          the effect of adding or or'ing the imm13 field to rs1.  */
  145.       int imm_added_to_rs1 = 0;
  146.  
  147.       /* Nonzero means that we have found a plus sign in the args
  148.          field of the opcode table.  */
  149.       int found_plus = 0;
  150.       
  151.       /* Do we have an `add' or `or' instruction where rs1 is the same
  152.          as rsd, and which has the i bit set?  */
  153.       if ((opcode->match == 0x80102000 || opcode->match == 0x80002000)
  154.       /*              (or)                 (add)  */
  155.           && insn.rs1 == insn.rd)
  156.         imm_added_to_rs1 = 1;
  157.  
  158.       if (insn.rs1 != insn.rd
  159.           && strchr (opcode->args, 'r') != 0)
  160.           /* Can't do simple format if source and dest are different.  */
  161.           continue;
  162.  
  163.       (*info->fprintf_func) (stream, opcode->name);
  164.  
  165.       {
  166.         register CONST char *s;
  167.  
  168.         if (opcode->args[0] != ',')
  169.           (*info->fprintf_func) (stream, " ");
  170.         for (s = opcode->args; *s != '\0'; ++s)
  171.           {
  172.         while (*s == ',')
  173.           {
  174.             (*info->fprintf_func) (stream, ",");
  175.             ++s;
  176.             switch (*s) {
  177.             case 'a':
  178.               (*info->fprintf_func) (stream, "a");
  179.               ++s;
  180.               continue;
  181.  
  182.             default:
  183.               break;
  184.             }        /* switch on arg */
  185.           }        /* while there are comma started args */
  186.  
  187.         (*info->fprintf_func) (stream, " ");
  188.             
  189.         switch (*s)
  190.           {
  191.           case '+':
  192.             found_plus = 1;
  193.  
  194.             /* note fall-through */
  195.           default:
  196.             (*info->fprintf_func) (stream, "%c", *s);
  197.             break;
  198.  
  199.           case '#':
  200.             (*info->fprintf_func) (stream, "0");
  201.             break;
  202.  
  203. #define    reg(n)    (*info->fprintf_func) (stream, "%%%s", reg_names[n])
  204.           case '1':
  205.           case 'r':
  206.             reg (insn.rs1);
  207.             break;
  208.  
  209.           case '2':
  210.             reg (insn.rs2);
  211.             break;
  212.  
  213.           case 'd':
  214.             reg (insn.rd);
  215.             break;
  216. #undef    reg
  217.  
  218. #define    freg(n)    (*info->fprintf_func) (stream, "%%%s", freg_names[n])
  219.           case 'e':
  220.           case 'v':    /* double/even */
  221.           case 'V':    /* quad/multiple of 4 */
  222.             freg (insn.rs1);
  223.             break;
  224.  
  225.           case 'f':
  226.           case 'B':    /* double/even */
  227.           case 'R':    /* quad/multiple of 4 */
  228.             freg (insn.rs2);
  229.             break;
  230.  
  231.           case 'g':
  232.           case 'H':    /* double/even */
  233.           case 'J':    /* quad/multiple of 4 */
  234.             freg (insn.rd);
  235.             break;
  236. #undef    freg
  237.  
  238. #define    creg(n)    (*info->fprintf_func) (stream, "%%c%u", (unsigned int) (n))
  239.           case 'b':
  240.             creg (insn.rs1);
  241.             break;
  242.  
  243.           case 'c':
  244.             creg (insn.rs2);
  245.             break;
  246.  
  247.           case 'D':
  248.             creg (insn.rd);
  249.             break;
  250. #undef    creg
  251.  
  252.           case 'h':
  253.             (*info->fprintf_func) (stream, "%%hi(%#x)",
  254.                        (int) insn.imm22 << 10);
  255.             break;
  256.  
  257.           case 'i':
  258.             {
  259.               /* We cannot trust the compiler to sign-extend
  260.              when extracting the bitfield, hence the shifts.  */
  261.               int imm = ((int) insn.imm13 << 19) >> 19;
  262.  
  263.               /* Check to see whether we have a 1+i, and take
  264.              note of that fact.
  265.  
  266.              Note: because of the way we sort the table,
  267.              we will be matching 1+i rather than i+1,
  268.              so it is OK to assume that i is after +,
  269.              not before it.  */
  270.               if (found_plus)
  271.             imm_added_to_rs1 = 1;
  272.               
  273.               if (imm <= 9)
  274.             (*info->fprintf_func) (stream, "%d", imm);
  275.               else
  276.             (*info->fprintf_func) (stream, "%#x", imm);
  277.             }
  278.             break;
  279.  
  280.  
  281.           case 'M':
  282.             (*info->fprintf_func) (stream, "%%asr%d", insn.rs1);
  283.             break;
  284.             
  285.           case 'm':
  286.             (*info->fprintf_func) (stream, "%%asr%d", insn.rd);
  287.             break;
  288.             
  289.           case 'L':
  290.             (*info->print_address_func)
  291.              ((bfd_vma) memaddr + insn.disp30 * 4,
  292.               info);
  293.             break;
  294.  
  295.           case 'l':
  296.             if ((insn.code >> 22) == 0)
  297.               /* Special case for `unimp'.  Don't try to turn
  298.              it's operand into a function offset.  */
  299.               (*info->fprintf_func)
  300.             (stream, "%#x",
  301.              (int) (((int) insn.disp22 << 10) >> 10));
  302.             else
  303.               /* We cannot trust the compiler to sign-extend
  304.              when extracting the bitfield, hence the shifts.  */
  305.               (*info->print_address_func)
  306.             ((bfd_vma) (memaddr
  307.                     + (((int) insn.disp22 << 10) >> 10) * 4),
  308.              info);
  309.             break;
  310.  
  311.           case 'A':
  312.             (*info->fprintf_func) (stream, "(%d)", (int) insn.asi);
  313.             break;
  314.  
  315.           case 'C':
  316.             (*info->fprintf_func) (stream, "%%csr");
  317.             break;
  318.  
  319.           case 'F':
  320.             (*info->fprintf_func) (stream, "%%fsr");
  321.             break;
  322.  
  323.           case 'p':
  324.             (*info->fprintf_func) (stream, "%%psr");
  325.             break;
  326.  
  327.           case 'q':
  328.             (*info->fprintf_func) (stream, "%%fq");
  329.             break;
  330.  
  331.           case 'Q':
  332.             (*info->fprintf_func) (stream, "%%cq");
  333.             break;
  334.  
  335.           case 't':
  336.             (*info->fprintf_func) (stream, "%%tbr");
  337.             break;
  338.  
  339.           case 'w':
  340.             (*info->fprintf_func) (stream, "%%wim");
  341.             break;
  342.  
  343.           case 'y':
  344.             (*info->fprintf_func) (stream, "%%y");
  345.             break;
  346.           }
  347.           }
  348.       }
  349.  
  350.       /* If we are adding or or'ing something to rs1, then
  351.          check to see whether the previous instruction was
  352.          a sethi to the same register as in the sethi.
  353.          If so, attempt to print the result of the add or
  354.          or (in this context add and or do the same thing)
  355.          and its symbolic value.  */
  356.       if (imm_added_to_rs1)
  357.         {
  358.           union sparc_insn prev_insn;
  359.           int errcode;
  360.  
  361.           errcode =
  362.         (*info->read_memory_func)
  363.           (memaddr - 4,
  364.            (bfd_byte *)&prev_insn, sizeof (prev_insn), info);
  365.  
  366.           if (errcode == 0)
  367.         {
  368.           /* If it is a delayed branch, we need to look at the
  369.              instruction before the delayed branch.  This handles
  370.              sequences such as
  371.  
  372.              sethi %o1, %hi(_foo), %o1
  373.              call _printf
  374.              or %o1, %lo(_foo), %o1
  375.              */
  376.  
  377.           if (is_delayed_branch (prev_insn))
  378.             errcode = (*info->read_memory_func)
  379.               (memaddr - 8, (bfd_byte *)&prev_insn, sizeof (prev_insn),
  380.                info);
  381.         }
  382.  
  383.           /* If there was a problem reading memory, then assume
  384.          the previous instruction was not sethi.  */
  385.           if (errcode == 0)
  386.         {
  387.           /* Is it sethi to the same register?  */
  388.           if ((prev_insn.code & 0xc1c00000) == 0x01000000
  389.               && prev_insn.rd == insn.rs1)
  390.             {
  391.               (*info->fprintf_func) (stream, "\t! ");
  392.               /* We cannot trust the compiler to sign-extend
  393.              when extracting the bitfield, hence the shifts.  */
  394.               (*info->print_address_func)
  395.             (((int) prev_insn.imm22 << 10)
  396.              | (insn.imm13 << 19) >> 19,
  397.              info);
  398.             }
  399.         }
  400.         }
  401.  
  402.       return sizeof (insn);
  403.     }
  404.     }
  405.  
  406.   (*info->fprintf_func) (stream, "%#8x", insn.code);
  407.   return sizeof (insn);
  408. }
  409.  
  410. /* Compare opcodes A and B.  */
  411.  
  412. static int
  413. compare_opcodes (a, b)
  414.      char *a, *b;
  415. {
  416.   struct sparc_opcode *op0 = (struct sparc_opcode *) a;
  417.   struct sparc_opcode *op1 = (struct sparc_opcode *) b;
  418.   unsigned long int match0 = op0->match, match1 = op1->match;
  419.   unsigned long int lose0 = op0->lose, lose1 = op1->lose;
  420.   register unsigned int i;
  421.  
  422.   /* If a bit is set in both match and lose, there is something
  423.      wrong with the opcode table.  */
  424.   if (match0 & lose0)
  425.     {
  426.       fprintf (stderr, "Internal error:  bad sparc-opcode.h: \"%s\", %#.8lx, %#.8lx\n",
  427.            op0->name, match0, lose0);
  428.       op0->lose &= ~op0->match;
  429.       lose0 = op0->lose;
  430.     }
  431.  
  432.   if (match1 & lose1)
  433.     {
  434.       fprintf (stderr, "Internal error: bad sparc-opcode.h: \"%s\", %#.8lx, %#.8lx\n",
  435.            op1->name, match1, lose1);
  436.       op1->lose &= ~op1->match;
  437.       lose1 = op1->lose;
  438.     }
  439.  
  440.   /* Because the bits that are variable in one opcode are constant in
  441.      another, it is important to order the opcodes in the right order.  */
  442.   for (i = 0; i < 32; ++i)
  443.     {
  444.       unsigned long int x = 1 << i;
  445.       int x0 = (match0 & x) != 0;
  446.       int x1 = (match1 & x) != 0;
  447.  
  448.       if (x0 != x1)
  449.     return x1 - x0;
  450.     }
  451.  
  452.   for (i = 0; i < 32; ++i)
  453.     {
  454.       unsigned long int x = 1 << i;
  455.       int x0 = (lose0 & x) != 0;
  456.       int x1 = (lose1 & x) != 0;
  457.  
  458.       if (x0 != x1)
  459.     return x1 - x0;
  460.     }
  461.  
  462.   /* They are functionally equal.  So as long as the opcode table is
  463.      valid, we can put whichever one first we want, on aesthetic grounds.  */
  464.  
  465.   /* Our first aesthetic ground is that aliases defer to real insns.  */
  466.   {
  467.     int alias_diff = (op0->flags & F_ALIAS) - (op1->flags & F_ALIAS);
  468.     if (alias_diff != 0)
  469.       /* Put the one that isn't an alias first.  */
  470.       return alias_diff;
  471.   }
  472.  
  473.   /* Except for aliases, two "identical" instructions had
  474.      better have the same opcode.  This is a sanity check on the table.  */
  475.   i = strcmp (op0->name, op1->name);
  476.   if (i)
  477.       if (op0->flags & F_ALIAS) /* If they're both aliases, be arbitrary. */
  478.       return i;
  479.       else
  480.       fprintf (stderr,
  481.            "Internal error: bad sparc-opcode.h: \"%s\" == \"%s\"\n",
  482.            op0->name, op1->name);
  483.  
  484.   /* Fewer arguments are preferred.  */
  485.   {
  486.     int length_diff = strlen (op0->args) - strlen (op1->args);
  487.     if (length_diff != 0)
  488.       /* Put the one with fewer arguments first.  */
  489.       return length_diff;
  490.   }
  491.  
  492.   /* Put 1+i before i+1.  */
  493.   {
  494.     char *p0 = (char *) strchr(op0->args, '+');
  495.     char *p1 = (char *) strchr(op1->args, '+');
  496.  
  497.     if (p0 && p1)
  498.       {
  499.     /* There is a plus in both operands.  Note that a plus
  500.        sign cannot be the first character in args,
  501.        so the following [-1]'s are valid.  */
  502.     if (p0[-1] == 'i' && p1[1] == 'i')
  503.       /* op0 is i+1 and op1 is 1+i, so op1 goes first.  */
  504.       return 1;
  505.     if (p0[1] == 'i' && p1[-1] == 'i')
  506.       /* op0 is 1+i and op1 is i+1, so op0 goes first.  */
  507.       return -1;
  508.       }
  509.   }
  510.  
  511.   /* They are, as far as we can tell, identical.
  512.      Since qsort may have rearranged the table partially, there is
  513.      no way to tell which one was first in the opcode table as
  514.      written, so just say there are equal.  */
  515.   return 0;
  516. }
  517.